/*
 * @Author: liqiushi
 * @Date: 2022-07-15 14:39:32
 *
 * 精选题库状态管理模块
 */

import { getCatalogIDBody, getRandomsList, getSubjectIDBody, getTagsBody } from '@/api/hmmm/questions'
import { simple } from '@/api/base/users'

const state = {
  // 学科下拉表单
  subjectIDBody: [],
  // 学科二级目录
  catalogIDBody: [],
  // 标签下拉菜单
  tagsBody: [],
  // 录入人下拉菜单
  creatorBody: [],
  // 头部表单体
  questionRandomsForm: {
    // 学科下拉列表绑定的数据
    subjectID: '',
    catalogID: '',
    tags: '',
    keyword: '',
    questionType: '',
    difficulty: '',
    direction: '',
    creator: '',
    remarks: '',
    shortName: '',
    city: '',
    province: ''
  },
  // 精选题库主体
  randomsList: [],
  // 发送请求的对象
  randomsListParams: {
    // 学科索引值
    subjectID: '',
    // 关键词搜索
    keyword: '',
    page: 1,
    pagesize: 5,
    chkState: ''
    /*
                                                            *chkState状态说明
                                                            * 全部---chkState为''
                                                            * 待审核---chkState为0
                                                            * 已审核---chkState为1
                                                            * 已拒绝---chkState为2
                                                            * */
  },
  counts: 0
}

const mutations = {
  setSubjectIDBody (state, playLoad) {
    state.subjectIDBody = playLoad
  },
  setCatalogIDBody (state, playLoad) {
    state.catalogIDBody = playLoad
  },
  setTagsBody (state, playLoad) {
    state.tagsBody = playLoad
  },
  setCreatorBody (state, playLoad) {
    state.creatorBody = playLoad
  },
  setQuestionRandomsForm (state, playLoad) {
    state.questionRandomsForm = playLoad
  },
  setRandomsList (state, playLoad) {
    state.randomsList = playLoad
  },
  // 修改发送参数的方法
  setRandomsListParams (state, playLoad) {
    Object.keys(playLoad).forEach(item => {
      // 如果参数列表中存在item这个key值 就将对应的value设置为它的
      state.randomsListParams[item] = playLoad[item]
    })
  },
  // 修改数据总数的方法
  setCounts (state, playLoad) {
    state.counts = playLoad
  }
}
const actions = {
  async getAsyncSubjectIDBody ({ commit }) {
    const { data } = await getSubjectIDBody()
    commit('setSubjectIDBody', data)
  },
  async getAsyncCatalogIDBody ({ commit }) {
    const { data } = await getCatalogIDBody()
    commit('setCatalogIDBody', data)
  },
  async getAsyncTagsBody ({ commit }) {
    const { data } = await getTagsBody()
    commit('setTagsBody', data)
  },
  async getAsyncCreatorBody ({ commit }) {
    const { data } = await simple()
    commit('setCreatorBody', data)
  },
  async getAsyncRandomsList ({ commit }) {
    const { data } = await getRandomsList({ page: 1, pagesize: 5 })
    // console.log(data)
    commit('setCounts', data.counts)
    commit('setRandomsList', data.items)
  },
  /*
                              * 设计 搜索等请求 直需要修改vuex里的参数 通过setRandomsListParams 通过过滤发送请求
                              * 核心思路 : 点击搜索直接过来发送请求 动态监听学科 关键字的改变 动态绑定randomsListParams中的参数
                              * 第二个参数传递一个 属性名 = 属性值 之后将其进行合并
                              * playLoad是一个对象
                              * 废弃上述思路
                              * 新的思路 通过mutation进行对randomlistparams的修改
                              * */
  async getAsyncRandomsListParams ({ state, commit }, playLoad) {
    Object.assign(state.randomsListParams, playLoad)
    const params = state.randomsListParams
    Object.keys(params).forEach(function (key) {
      // 如果对象中的值为空 则删除
      if (!params[key]) {
        delete params[key]
      }
    })
    // 发送请求
    const { data } = await getRandomsList(params)
    await commit('setCounts', data.counts)
    await commit('setRandomsList', data.items)
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
